package com.lanou.myapplication.PhotoViewpagerFragement;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.lanou.myapplication.R;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by dllo on 15/10/23.
 */
public class PhotoFragment extends Fragment {
    private ImageView centerImageView;//中心图片
    private TextView imagecontent,currentPage,sumPage;//内容文字
    private String alt;//内容
    private String pic;//图片地址
    private int position;
    private  int sum;

    private RequestQueue queue;//定义请求队列

    //单例模式 整个应用中保证只有一个实例对象 且必须是通过静态方法获取
    private static PhotoFragment photoFragment;

    public PhotoFragment() {

    }

    public static PhotoFragment getInstance(String alt,
                                               String pic,int position,int sum) {

        PhotoFragment photoFragment =
                new PhotoFragment();

        //参数传递
        Bundle args = new Bundle();
        args.putString("alt", alt);
        args.putString("pic", pic);
        args.putInt("position", position);
        args.putInt("sum",sum);
        photoFragment.setArguments(args);

        return photoFragment;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        //初始化请求队列
        queue = Volley.newRequestQueue(activity);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //获取参数
        Bundle bundle =getArguments();
        if(bundle !=null){
            alt=bundle.getString("alt");
            pic=bundle.getString("pic");
            position= bundle.getInt("position");
            sum = bundle.getInt("sum");


        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       //加载布局文件
        View v= inflater.inflate(R.layout.image_photoviewpager,null);
        centerImageView =(ImageView)v.findViewById(R.id.centerImageView);
        imagecontent=(TextView)v.findViewById(R.id.imagecontent);
        currentPage=(TextView)v.findViewById(R.id.currentPage);
        sumPage=(TextView)v.findViewById(R.id.sumPage);
        currentPage.setText(position+1+"");
        sumPage.setText(sum+"");
        imagecontent.setText(alt);
        //网络拉去图片
        getBitmap(pic,centerImageView);
        return v;
    }
    /**
     * 通过Volley请求图片
     * @param url 图片地址
     * @param imageView 要显示的图片组件
     * **/
    public void getBitmap(String url, final ImageView imageView) {

        ImageRequest request = new ImageRequest(url,
                new Response.Listener<Bitmap>() {

                    @Override
                    public void onResponse(Bitmap response) {
                        //将返回的bitmap图片对象设置到图片组件中
                        imageView.setImageBitmap(response);
                    }
                },
                634, 400, Bitmap.Config.ARGB_8888,
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(
                            VolleyError error) {
                        error.printStackTrace();
                    }
                }){
            @Override
            public Map<String, String> getHeaders()
                    throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };

        queue.add(request);//将本次图片请求加入到请求队列中
    }

}
